﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Common;
using CNCSoft.EData;

namespace CNCSoft.ErpApps.TMS.Test
{
    public partial class frmKetQuaThiTracNghiem : Form
    {
        SqlConnection conn = new SqlConnection(GlobalVariable.WSInfo.ConnectionString);
        DataSet ds = new DataSet();
        private string _enquirynumber = "", _madethi = "";

        public frmKetQuaThiTracNghiem()
        {
            InitializeComponent();
        }

        private void frmKetQuaThiTracNghiem_Load(object sender, EventArgs e)
        {
            LoadControl();
        }

        private void searchLookUpEdit_KhachHang_EditValueChanged(object sender, EventArgs e)
        {
            _enquirynumber = searchLookUpEdit_KhachHang.EditValue.ToString();
            LoadDeThiByEnquiry(_enquirynumber);
            searchLookUpEdit_DeThi.Properties.DataSource = ds.Tables["TableDeThiByEnquiry"];
        }

        private void searchLookUpEdit_DeThi_EditValueChanged(object sender, EventArgs e)
        {
            _madethi = searchLookUpEdit_DeThi.EditValue.ToString();
        }

        private void btnXem_Click(object sender, EventArgs e)
        {
            if (_madethi != "" && _enquirynumber != "")
            {
                AddQuestion(_enquirynumber, _madethi);
            }
        }

        #region "Coding"
        private void LoadControl()
        {
            try
            {
                LoadEnquiry();
                searchLookUpEdit_KhachHang.Properties.DataSource = ds.Tables["TableEnquiry"];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void LoadEnquiry()
        {
            try
            {
                string sql = @"select EnquiryNumber, (Holot + ' ' + Ten) as HoTen
                                from Enquiry";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                da.Fill(ds, "TableEnquiry");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void LoadDeThiByEnquiry(string enquirynumber)
        {
            try
            {
                if (ds.Tables["TableDeThiByEnquiry"] != null)
                    ds.Tables["TableDeThiByEnquiry"].Clear();
                string sql = @"
                                select distinct EnquiryNumber, MaDeThi
                                from ResultPlacementTest
                                where EnquiryNumber = '" + enquirynumber + "'";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                da.Fill(ds, "TableDeThiByEnquiry");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void AddQuestion(string enquirynumber, string madethi)
        {
            try
            {
                this.SuspendLayout();
                SqlDataReader De_Reader = SQLHelper.ExecuteReader(GlobalVariable.WSInfo.ConnectionString, 
                                                                    "usp_AllResultPlacementTestByEnquiryAndMaDeThi @enquirynumber = '" + enquirynumber + @"', 
                                                                        @madethi = '" + madethi + "'");
                int STT = 0;
                int intGiaSoY = 0;

                while (De_Reader.Read())
                {
                    try
                    {
                        STT++;
                        int STT_CauHoi = Convert.ToInt32(De_Reader["MaCauHoi"].ToString().Substring(2, 4));
                        if (STT_CauHoi == 1)
                        {
                            if (STT > 1) STT++;
                            AddPhanGioiThieu(De_Reader["MaCauHoi"].ToString().Substring(1, 1), (STT - 1) * 100 - 20);
                        }
                        try
                        {
                            intGiaSoY = Convert.ToInt32(De_Reader["CachTrinhBay"]);
                        }
                        catch (Exception ex)
                        {
                            intGiaSoY = 0;
                        }

                        if (De_Reader["CachTrinhBay"].ToString() == "" || (De_Reader["CachTrinhBay"].ToString().Substring(0, 1) != "R" && De_Reader["CachTrinhBay"].ToString().Substring(0, 1) != "W"))
                            AddControlSingle(De_Reader["MaCauHoi"].ToString(),
                                               (STT - 1) * 100 + 80 + intGiaSoY,
                                               STT_CauHoi.ToString("G") + ". " + De_Reader["NoiDungCauHoi"].ToString().Trim(),
                                               De_Reader["NoiDungCauA"].ToString().Trim(),
                                               De_Reader["NoiDungCauB"].ToString().Trim(),
                                               De_Reader["NoiDungCauC"].ToString().Trim(),
                                               De_Reader["NoiDungCauD"].ToString().Trim(),
                                               De_Reader["CachTrinhBay"].ToString().Trim(),
                                               De_Reader["TraLoi"].ToString(),
                                               De_Reader["DapAn"].ToString());
                        else if (De_Reader["CachTrinhBay"].ToString().Substring(0, 1) == "R" || De_Reader["CachTrinhBay"].ToString().Substring(0, 1) == "W")
                            AddControlMultiReader(De_Reader["MaCauHoi"].ToString(),
                                                   (STT - 1) * 100 + 80,
                                                   STT_CauHoi.ToString("G") + ". " + De_Reader["NoiDungCauHoi"].ToString().Trim(),
                                                   De_Reader["NoiDungCauA"].ToString().Trim(),
                                                   De_Reader["NoiDungCauB"].ToString().Trim(),
                                                   De_Reader["NoiDungCauC"].ToString().Trim(),
                                                   De_Reader["NoiDungCauD"].ToString().Trim(),
                                                   De_Reader["CachTrinhBay"].ToString().Trim(),
                                                   De_Reader["TraLoi"].ToString(),
                                                   De_Reader["DapAn"].ToString());

                        //-------------------------------------------------
                        //TinhDiem();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
                //Dong ket thuc
                AddPhanGioiThieu("E", STT * 100 + 100 + intGiaSoY);

                this.ResumeLayout(false);
                this.PerformLayout();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void AddPhanGioiThieu(string TheLoai, int intY)
        {
            try
            {
                Label lblNoiDungCauHoi = new Label();
                lblNoiDungCauHoi.AutoSize = false;
                lblNoiDungCauHoi.Width = this.Width - 200;
                lblNoiDungCauHoi.Height = 32;
                lblNoiDungCauHoi.Location = new System.Drawing.Point(40, intY);
                lblNoiDungCauHoi.Font = new Font("Arial", 12);
                lblNoiDungCauHoi.Font = new Font(lblNoiDungCauHoi.Font, FontStyle.Bold);
                lblNoiDungCauHoi.Text = GlobalVariable.WSInfo.getAppProperty("English.Intro_" + TheLoai, "");
                this.panelMain.Controls.Add(lblNoiDungCauHoi);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void AddControlSingle(string strMaCauHoi, int intY, string strNoiDungCauHoi, string strA, string strB, string strC, string strD, string strCachTrinhBay, string strTraLoi, string strDapAn)
        {
            try
            {
                Label lblNoiDungCauHoi = new Label();
                lblNoiDungCauHoi.AutoSize = false;
                lblNoiDungCauHoi.Width = this.Width - 50;
                lblNoiDungCauHoi.Height = 32;
                lblNoiDungCauHoi.Location = new System.Drawing.Point(40, intY);
                lblNoiDungCauHoi.Name = strMaCauHoi;
                lblNoiDungCauHoi.TabIndex = 0;
                lblNoiDungCauHoi.Text = strNoiDungCauHoi;
                this.panelMain.Controls.Add(lblNoiDungCauHoi);

                if ((strA + strB + strC + strD) == "") return;

                //Group A/B/C/D
                Panel grpABCD = new Panel();
                grpABCD.AutoSize = true;
                grpABCD.Location = new System.Drawing.Point(lblNoiDungCauHoi.Location.X + 10, lblNoiDungCauHoi.Location.Y + 10);
                grpABCD.AutoSizeMode = AutoSizeMode.GrowAndShrink;
                grpABCD.Name = "grp" + strMaCauHoi;
                grpABCD.Tag = "";
                this.panelMain.Controls.Add(grpABCD);

                //Noi dung tung cau A
                RadioButton NoiDungCauA = new RadioButton();
                NoiDungCauA.AutoSize = true;
                NoiDungCauA.Location = new Point(10, 20);
                NoiDungCauA.Text = "a." + strA;
                NoiDungCauA.Name = "A_" + strMaCauHoi;
                if (strA != "")
                    this.panelMain.Controls["grp" + strMaCauHoi].Controls.Add(NoiDungCauA);

                //Noi dung tung cau B
                RadioButton NoiDungCauB = new RadioButton();
                NoiDungCauB.AutoSize = true;
                NoiDungCauB.Location = new Point(400, NoiDungCauA.Location.Y);
                NoiDungCauB.Text = "b." + strB;
                NoiDungCauB.Name = "B_" + strMaCauHoi;
                if (strB != "")
                    this.panelMain.Controls["grp" + strMaCauHoi].Controls.Add(NoiDungCauB);

                //Noi dung tung cau C
                RadioButton NoiDungCauC = new RadioButton();
                NoiDungCauC.AutoSize = true;
                NoiDungCauC.Location = new Point(800, NoiDungCauA.Location.Y);
                NoiDungCauC.Text = "c." + strC;
                NoiDungCauC.Name = "C_" + strMaCauHoi;
                if (strC != "")
                    this.panelMain.Controls["grp" + strMaCauHoi].Controls.Add(NoiDungCauC);

                //Noi dung tung cau D
                RadioButton NoiDungCauD = new RadioButton();
                NoiDungCauD.AutoSize = true;
                NoiDungCauD.Location = new Point(1200, NoiDungCauA.Location.Y);
                NoiDungCauD.Text = "d." + strD;
                NoiDungCauD.Name = "D_" + strD;
                if (strD != "")
                    this.panelMain.Controls["grp" + strMaCauHoi].Controls.Add(NoiDungCauD);
                //----------------------------------------------
                if (strTraLoi != "" && strTraLoi != null)
                {
                    if (strTraLoi.ToLower() == "a")
                        NoiDungCauA.Checked = true;
                    else if (strTraLoi.ToLower() == "b")
                        NoiDungCauB.Checked = true;
                    else if (strTraLoi.ToLower() == "c")
                        NoiDungCauC.Checked = true;
                    else if (strTraLoi.ToLower() == "d")
                        NoiDungCauD.Checked = true;
                }
                if (strTraLoi.ToLower() != strDapAn.ToLower())
                {
                    this.panelMain.Controls["grp" + strMaCauHoi].BackColor = Color.Blue;
                    this.panelMain.Controls["grp" + strMaCauHoi].ForeColor = Color.White;
                }
                else
                {
                    this.panelMain.Controls["grp" + strMaCauHoi].BackColor = Color.Red;
                    this.panelMain.Controls["grp" + strMaCauHoi].ForeColor = Color.Yellow;
                }
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void AddControlMultiReader(string strMaCauHoi, int intY, string strNoiDungCauHoi, string strA, string strB, string strC, string strD, string strCachTrinhBay, string strTraLoi, string strDapAn)
        {
            try
            {
                int intvitriABCD = 0;
                //Phan noi dung bai doc ben trái
                if (strCachTrinhBay == "R0") //Chi add panel cho cau lien tuc dau tien
                {
                    panelBaiDoc.Visible = true;
                    panelBaiDoc.Width = 800;
                    panelBaiDoc.Top = intY;
                    panelBaiDoc.Left = 40;
                    intvitriABCD = intY - 10;
                    richTextBoxBaiDoc.LoadFile(Environment.CurrentDirectory + "\\PlacementReading" + _madethi + ".rtf");
                }
                else
                {
                    intvitriABCD = panelBaiDoc.Top - 10 + (Convert.ToInt32(strCachTrinhBay.Substring(1))) * 32;
                }
                panelBaiDoc.Height = intvitriABCD - panelBaiDoc.Top + 500;

                if (strCachTrinhBay.Substring(0, 1).ToLower() == "w")
                {
                    //Phan textbox o duoi panelbaidoc
                    //GroupBox grpABCD = new GroupBox();
                    Panel grpABCD = new Panel();
                    grpABCD.AutoSize = true;
                    grpABCD.Location = new Point(40, panelBaiDoc.Bottom + 300);
                    grpABCD.AutoSizeMode = AutoSizeMode.GrowAndShrink;
                    grpABCD.Name = "grp" + strMaCauHoi;
                    grpABCD.Tag = "";
                    this.panelMain.Controls.Add(grpABCD);
                    //Label so cau hoi truoc A/B/C/D
                    Label labelSoCauHoi = new Label();
                    labelSoCauHoi.Location = new Point(10, 22);
                    labelSoCauHoi.AutoSize = true;
                    labelSoCauHoi.Text = strNoiDungCauHoi;
                    this.panelMain.Controls["grp" + strMaCauHoi].Controls.Add(labelSoCauHoi);
                    //---------------------textbox------------------------------------------------------
                    TextBox Writing = new TextBox();
                    Writing.Width = 300;
                    Writing.Height = 32;
                    Writing.Location = new Point(labelSoCauHoi.PreferredWidth + 10, 20);
                    Writing.Text = "";
                    Writing.Name = "--" + strMaCauHoi;
                    this.panelMain.Controls["grp" + strMaCauHoi].Controls.Add(Writing);
                    if (strTraLoi != "" && strTraLoi != null)
                    {
                        Writing.Text = strTraLoi;
                    }
                }
                else
                {
                    //Phan A/B/C/D ben phai
                    Panel grpABCD = new Panel();
                    grpABCD.AutoSize = true;
                    grpABCD.Location = new Point(panelBaiDoc.Left + panelBaiDoc.Width + 20, intvitriABCD + 10);
                    grpABCD.AutoSizeMode = AutoSizeMode.GrowAndShrink;
                    grpABCD.Name = "grp" + strMaCauHoi;
                    grpABCD.Tag = "";
                    this.panelMain.Controls.Add(grpABCD);

                    //Label so cau hoi truoc A/B/C/D
                    Label labelSoCauHoi = new Label();
                    labelSoCauHoi.Location = new Point(10, 22);
                    labelSoCauHoi.AutoSize = true;
                    labelSoCauHoi.Text = strNoiDungCauHoi.Substring(0, 3);
                    this.panelMain.Controls["grp" + strMaCauHoi].Controls.Add(labelSoCauHoi);

                    //Noi dung tung cau A  (truoc mat cho TH single choise)
                    RadioButton NoiDungCauA = new RadioButton();
                    NoiDungCauA.AutoSize = true;
                    NoiDungCauA.Location = new Point(40, 20);
                    if (strA.ToLower() != "a")
                    {
                        NoiDungCauA.Text = "a. " + strA;
                    }
                    else
                    {
                        NoiDungCauA.Text = "a.";
                    }
                    NoiDungCauA.Name = "A_" + strMaCauHoi;
                    if (strA != "")
                        this.panelMain.Controls["grp" + strMaCauHoi].Controls.Add(NoiDungCauA);

                    //Noi dung tung cau B 
                    RadioButton NoiDungCauB = new RadioButton();
                    NoiDungCauB.AutoSize = true;
                    NoiDungCauB.Location = new Point(160, NoiDungCauA.Location.Y);
                    if (strB.ToLower() != "b")
                    {
                        NoiDungCauB.Text = "b. " + strB;
                    }
                    else
                    {
                        NoiDungCauB.Text = "b.";
                    }
                    NoiDungCauB.Name = "B_" + strMaCauHoi;
                    if (strB != "")
                        this.panelMain.Controls["grp" + strMaCauHoi].Controls.Add(NoiDungCauB);

                    //Noi dung tung cau C 
                    RadioButton NoiDungCauC = new RadioButton();
                    NoiDungCauC.AutoSize = true;
                    NoiDungCauC.Location = new Point(300, NoiDungCauA.Location.Y);
                    if (strC.ToLower() != "c")
                    {
                        NoiDungCauC.Text = "c. " + strC;
                    }
                    else
                    {
                        NoiDungCauC.Text = "c.";
                    }
                    NoiDungCauC.Name = "C_" + strMaCauHoi;
                    if (strC != "")
                        this.panelMain.Controls["grp" + strMaCauHoi].Controls.Add(NoiDungCauC);

                    //------------------
                    if (strTraLoi != "" && strTraLoi != null)
                    {
                        if (strTraLoi.ToLower() == "a")
                            NoiDungCauA.Checked = true;
                        else if (strTraLoi.ToLower() == "b")
                            NoiDungCauB.Checked = true;
                        else if (strTraLoi.ToLower() == "c")
                            NoiDungCauC.Checked = true;
                    }
                    if (strTraLoi.ToLower() != strDapAn.ToLower())
                    {
                        this.panelMain.Controls["grp" + strMaCauHoi].BackColor = Color.Blue;
                        this.panelMain.Controls["grp" + strMaCauHoi].ForeColor = Color.White;
                    }
                    else
                    {
                        this.panelMain.Controls["grp" + strMaCauHoi].BackColor = Color.Red;
                        this.panelMain.Controls["grp" + strMaCauHoi].ForeColor = Color.Yellow;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void TinhDiem()
        {
            try
            {
                SqlDataReader De_DataReader = SQLHelper.ExecuteReader(GlobalVariable.WSInfo.ConnectionString,
                        "SELECT * FROM TEST_Question WHERE left(MaCauHoi,1) = 'P' AND MaDeThi = '" + _madethi + "'");  //Placement test
            int STT = 0;
            int intCorrectReading = 0;
            int intCorrectListening = 0;
            //while (De_DataReader.Read() && STT < 5)
            while (De_DataReader.Read())
            {
                try
                {
                    STT++;
                    string strMaCauHoi = De_DataReader["MaCauHoi"].ToString();
                    string strDapAn = De_DataReader["DapAn"].ToString().ToLower();
                    string strTheLoaiCauHoi = strMaCauHoi.Substring(1, 1);
                    /*
                    MessageBox.Show("Cau: " + this.panelMain.Controls["grp" + strMaCauHoi].Name +
                        "\nTra loi: " + this.panelMain.Controls["grp" + strMaCauHoi].Tag.ToString() +
                        "\nDap an: " + strDapAn) ;
                    */
                    if ( strTheLoaiCauHoi == "L" && this.panelMain.Controls["grp" + strMaCauHoi].Tag.ToString().ToLower() == strDapAn)
                        intCorrectListening++;
                    if (strTheLoaiCauHoi == "R" && this.panelMain.Controls["grp" + strMaCauHoi].Tag.ToString().ToLower() == strDapAn)
                        intCorrectReading++;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            } //End While

            lblKetQua.Text = "Listening:  " + intCorrectListening.ToString("G") + "  +  Reading:  " + intCorrectReading.ToString("G") +
                             "  =>  Total:  " + (intCorrectListening + intCorrectReading).ToString("G");
            lblRecommendedClass.Text = ""; //Tu van tu quyet// "Recommended class: " + Return_RecommendedTest(intCorrectListening + intCorrectReading);
            string strRecommendedClass = "Recommended class: " + Return_RecommendedTest(intCorrectListening + intCorrectReading);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private string Return_RecommendedTest(int TongsoDiem)
        {
            if (TongsoDiem <= 70)
                return "GE - Intro (Beginner)";
            else if (TongsoDiem <= 80)
                return "GE - English 1 (Beginner)";
            else if (TongsoDiem <= 90)
                return "GE - English 2 (Elementary)";
            else if (TongsoDiem <= 100)
                return "GE - English 3 (Elementary)";
            else if (TongsoDiem <= 120)
                return "GE - English 4 (Pre-Intermediate)";
            else if (TongsoDiem <= 130)
                return "GE - English 5 (Pre-Intermediate)";
            else if (TongsoDiem <= 140)
                return "GE - English 6 (Intermediate)";
            else if (TongsoDiem <= 150)
                return "GE - English 7 (Intermediate)";
            else if (TongsoDiem <= 160)
                return "GE - English 8 (Upper-Intermediate)";
            else if (TongsoDiem <= 200)
                return "GE - English 9 (Upper-Intermediate)";
            else
                return "";
        }
        #endregion
    }
}
